//
//  ZZCEmoticonToolBar.swift
//  weibo
//
//  Created by zhangzuchao on 2017/4/6.
//  Copyright © 2017年 zhangZuChao. All rights reserved.
//

import UIKit

//  表情类型
enum ZZCEmoticonType: Int {
    //  最近
    case recent = 10
    //  默认
    case normal = 11
    //  emoji
    case emoji = 12
    //  浪小花
    case lxh = 13
}
class ZZCEmoticonToolBar: UIStackView {
    
    //  已经选中的按钮
    var lastSelectedButton: UIButton?
    
    //  回调点击按钮的枚举类型
    var didSelectedEmoticonClosure: ((ZZCEmoticonType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加子控件设置子控件的填充方式
    private func setupUI() {
        //  水平方向布局 默认值
        self.axis = .horizontal
        //  子按钮等比填充
        self.distribution = .fillEqually
        
        addChildButton(bgImageName: "compose_emotion_table_left", title: "最近", type: .recent)
        addChildButton(bgImageName: "compose_emotion_table_mid", title: "默认", type: .normal)
        addChildButton(bgImageName: "compose_emotion_table_mid", title: "Emoji", type: .emoji)
        addChildButton(bgImageName: "compose_emotion_table_right", title: "浪小花", type: .lxh)
    }
    //  添加子按钮
    fileprivate func addChildButton(bgImageName: String, title: String, type: ZZCEmoticonType) {
        let button = UIButton()
        //  使用枚举的原始值作为tag
        button.tag = type.rawValue
        //  添加点击事件
        button.addTarget(self, action: #selector(btnAction(btn:)), for: .touchUpInside)
        //  设置不同状态的背景图片
        button.setBackgroundImage(UIImage(named: bgImageName + "_normal"), for: .normal)
        button.setBackgroundImage(UIImage(named: bgImageName + "_selected"), for: .selected)
        //  设置文字
        button.setTitle(title, for: .normal)
        //  设置文字颜色
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .selected)
        //  设置字体大小
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        //  取消高亮
        button.adjustsImageWhenHighlighted = false
        //  添加子按钮
        addArrangedSubview(button)
        //  默认表情选中
        if type == .normal {
            lastSelectedButton?.isSelected = false
            button.isSelected = true
            lastSelectedButton = button
        }
        
    }
    
    //   toolbar按钮点击事件处理
    @objc private func btnAction(btn: UIButton) {
        
        //  如果上一次选中的按钮和这次点击的一样,不让其再次选中
        if lastSelectedButton == btn {
            return
        }
        
        //点击互斥
        //  上一次的按钮取消选中
        lastSelectedButton?.isSelected = false
        
        //  点击的按钮选中
        btn.isSelected = true
        
        //  记录选中的按钮
        lastSelectedButton = btn
        
        //  根据tag值获取枚举
        let type = ZZCEmoticonType(rawValue: btn.tag)!
        
        //  回调点击的表情按钮的枚举
        didSelectedEmoticonClosure?(type)
       
    }

    //  根据组的索引选中对应的按钮
    func selectButton(section: Int) {
        //   0 获取的是当前控件自己,所以设置为10-20
        let button = viewWithTag(section + 10) as! UIButton
        //  如果选中按钮和上一次的按钮一样,不让其再次选中
        if button == lastSelectedButton {
            return
        }
        //  上一次的按钮取消选中
        lastSelectedButton?.isSelected = false
        //  这次的按钮选中
        button.isSelected = true
        //  记录选中的按钮
        lastSelectedButton = button
    }


}
